#include "junix.h"
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>

void remove_tags(std::string& s);
void replace_str(std::string& line, const std::string& from,const  std::string& to);

int main(int argc, char **argv)
{
	if( argc != 5 || 
			argv[1] != std::string("-f") || 
			argv[3] != std::string("-t") )	{
		std::cout<<"Usage:"<<argv[0]<<" -f HTML -t TXT"<<std::endl;
		exit(EXIT_FAILURE);
	}

	std::ifstream input(argv[2]);
	std::ofstream output(argv[4]);
	if(!input || !output){
		std::cerr<<"File open error"<<std::endl;
		exit(EXIT_FAILURE);
	}

	std::string line;
	bool prev_line_null = true;
	while( getline(input, line))
	{
		remove_tags(line);
	//	output<<line<<std::endl;

		if( line.empty() && prev_line_null)
			continue;

		if( line.empty() && !prev_line_null)
		{
			output<<line<<std::endl;
			prev_line_null = true;
		}

		if( !line.empty())
		{
			output<<line<<std::endl;
			prev_line_null = false;
		}
	}

	input.close();
	output.close();
}

void remove_tags(std::string& s)
{
	boost::trim(s);
	std::string::size_type beg, end;

	while(true)
	{
		beg = s.find("<");
		end = s.find(">");

		if(beg==std::string::npos &&
				end == std::string::npos){
			break;
		}
		else if( beg != std::string::npos &&
				end == std::string::npos){
			s.erase(beg, std::string::npos);
			break;
		}
		else if( beg == std::string::npos &&
				end != std::string::npos){
			s.erase(0,end+1);
		}else if( end>beg){
			s.erase(beg, end-beg+1);
		}else{
			s.erase(0,end+1);
		}
	}

	replace_str(s, std::string("&gt;"), std::string(">") );
	replace_str(s, std::string("&lt;"), std::string("<") );
	replace_str(s, std::string("&amp;"), std::string("&") );
	replace_str(s, std::string("&nbsp;"), std::string(" ") );
	replace_str(s, std::string("&ldquo;"), std::string("\"") );
	replace_str(s, std::string("&rdquo;"), std::string("\"") );
	replace_str(s, std::string("&mdash;"), std::string("-") );
	replace_str(s,std::string("  "), std::string(" "));

	boost::trim(s);
}


void replace_str(std::string& line, const std::string& from, const std::string& to)
{
	std::string::size_type pos, index=0;
	std::string::size_type fsize=from.size();
	std::string::size_type tsize=to.size();
	while(true)
	{
		pos = line.find(from,index);
		if( pos==std::string::npos)
			break;

		line.replace(pos,fsize,to); 
		index =  pos + tsize;
	}
}
